package ServerConnection;

import Client.ServerInfo;
import Message.AccountSignUpInfo;

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

public class AccountSignUpServer implements Runnable {
    DatabaseServer databaseServer;

    public AccountSignUpServer(DatabaseServer databaseServer) {
        this.databaseServer = databaseServer;
    }

    public AccountSignUpServer() {
        this.databaseServer = new DatabaseServer();
    }


    public void run() {
        try {
            ServerSocket serverSocket = new ServerSocket(ServerInfo.SERVER_SIGNUP_PORT);
            System.out.println("账号注册的服务成功启动！");
            Socket socket;
            while (true) {
                socket = serverSocket.accept();
                Thread handle = new Thread(new ThreadHandleAccountSignUp(socket, databaseServer));
                handle.start();
            }
        } catch (IOException e) {
            System.out.println("因socket创建失败，注册服务启动失败！");
        }
    }
}


class ThreadHandleAccountSignUp implements Runnable {
    Socket socket = null;
    AccountSignUpInfo info;
    boolean res;
    String uid;
    String password;
    DatabaseServer databaseServer;

    public ThreadHandleAccountSignUp(Socket socket, DatabaseServer databaseServer) {
        super();
        this.socket = socket;
        this.databaseServer = databaseServer;
    }

    @Override

    public void run() {
        try {
            System.out.println("收到账号注册请求，正在处理");
            //获取从客户端得到的消息，并且执行了消息。
            InputStream inputStream = socket.getInputStream();
            ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);
            info = (AccountSignUpInfo) objectInputStream.readObject();
            uid = info.getId();
            password = info.getPassword();
            String statement = String.format("INSERT INTO UserAccount(Uid,psw) VALUES (%s,%s);", uid, password);
            res = databaseServer.doSignUp(statement);
            System.out.println("执行" + res);
            //返还执行的结果。
            OutputStream outputStream = socket.getOutputStream();
            outputStream.write(String.valueOf(res).getBytes(StandardCharsets.UTF_8));

            inputStream.close();
            outputStream.close();
            socket.close();


        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}

class ASUSTEST {
    public static void main(String[] args) {
        try {
            DatabaseServer databaseServer = new DatabaseServer();
            ServerSocket serverSocket = new ServerSocket(ServerInfo.SERVER_SIGNUP_PORT);
            System.out.println("账号注册服务成功启动！");
            Socket socket;
            while (true) {
                socket = serverSocket.accept();
                Thread handle = new Thread(new ThreadHandleAccountSignUp(socket, databaseServer));
                handle.start();
            }
        } catch (IOException e) {
            System.out.println("因socket创建失败，注册服务启动失败！");
        }
    }
}